<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条面经</span>
          <el-button
            round
            size="small"
            icon="el-icon-plus"
            type="primary"
            @click="showDrawer('面经添加')"
            >添加面经</el-button
          >
        </div>
      </template>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="stem" label="标题" width="260">
        </el-table-column>
        <el-table-column prop="creator" label="作者"></el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间" width="160">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <div class="actions">
              <i
                class="el-icon-view"
                @click="showDrawer('面经预览', scope.row.id)"
              ></i>
              <i
                class="el-icon-edit-outline"
                @click="showDrawer('面经编辑', scope.row.id)"
              ></i>
              <i
                class="el-icon-delete"
                @click="deleteArticle(scope.row.id)"
              ></i>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-drawer
      :title="drawerTitle"
      size="46%"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="drawerClose"
    >
      <div v-if="drawerTitle==='面经预览'" class="preview">
        <h4>{{ form.stem }}</h4>
        <div class="content" v-html="form.content"></div>
      </div>
      <el-form v-else ref="form" label-width="80px" :model="form" :rules="rules">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import {
  deleteArticleItem,
  getArticleDetail,
  getArticleList,
  setArticleItem,
  updateArticle
} from '@/api/article'
export default {
  name: 'article-page',
  data() {
    return {
      tableData: [],
      current: 1,
      pageSize: 10,
      total: 0,
      drawer: false,
      title: '',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容' }]
      },
      drawerTitle: ''
    }
  },
  async created() {
    this.initData()
  },
  methods: {
    async initData() {
      const { data } = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      // console.log(data)
      this.current = data.current
      this.pageSize = data.pageSize
      this.total = data.total
      this.tableData = data.rows
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.current = 1
      this.initData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.current = val
      this.initData()
    },
    async showDrawer(title, id) {
      this.drawerTitle = title
      this.drawer = true
      if (this.drawerTitle !== '面经添加') {
        const res = await getArticleDetail(id)
        // console.log(res)
        this.form = { ...res.data }
      }
    },
    drawerClose() {
      this.drawer = false
      this.$refs.form.resetFields()
    },
    async onSubmit() {
      try {
        await this.$refs.form.validate()
        const { id, stem, content } = this.form
        if (this.drawerTitle === '面经添加') {
          await setArticleItem({ stem, content })
          this.$message.success('添加成功')
        }
        if (this.drawerTitle === '面经编辑') {
          await updateArticle({ id, stem, content })
          this.$message.success('修改成功')
        }
        this.current = 1
        this.initData()
        this.drawer = false
        this.$refs.form.resetFields()
      } catch (error) {
        console.log(error)
      }
    },
    async deleteArticle(id) {
      await deleteArticleItem(id)
      // console.log(res)
      this.$message.success('删除成功')
      this.initData()
    },
    cancel() {
      this.drawer = false
      this.$refs.form.resetFields()
    }
  },
  components: { quillEditor }
}
</script>

<style scoped lang="scss">
.el-card {
  margin-top: 15px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 20px;
  }
  .actions {
    font-size: 18px;
    color: #999;
    display: flex;
    justify-content: space-evenly;
    i:hover {
      cursor: pointer;
      color: rgba(114, 124, 245, 1);
    }
  }
  .el-pagination {
    text-align: center;
    margin-top: 20px;
  }
}
.el-form {
  padding-right: 40px;
  ::v-deep .ql-editor {
    height: 280px;
  }
}
.preview {
  padding: 0 40px;
  h4 {
    font-size: 18px;
    padding-bottom: 20px;
    border-bottom: 1px dashed #ccc;
  }
  .content {
    margin-top: 16px;
    line-height: 26px;
  }
}
</style>
